<template>
	<view style="padding-bottom: 180upx;">

		<view class="bg margin-lr radius margin-tb-sm">
			<view class="flex  padding-lr padding-tb-sm">
				<image :src="orderList.fixedClassImg" style="width: 149upx;height: 149upx;"></image>
				<view class="margin-left-xs">
					<view class="text-lg text-bold">{{orderList.serviceName}}</view>
					<view class="margin-tb-xs" style="color: #999999;">{{orderList.fixedClassifyName}}</view>
					<view class="flex text-sm ">上门时间：{{orderList.deliveryTime}}
					</view>
				</view>
			</view>
		</view>

		<view class="bg margin-lr radius margin-top-sm padding">
			<view>服务内容</view>
			<view class="margin-top" style="color: #999999;">{{orderList.content}}</view>
			<view class="flex flex-wrap">
				<view v-for="(ite,ind) in orderList.image" :key="ind" class="margin-top margin-right-sm"
					@click="saveImg(orderList.image,ind)">
					<image :src="ite" style="width: 160upx; height: 160upx;border-radius: 5upx;"></image>
				</view>
			</view>
		</view>

		<view class="flex align-center justify-between bg margin padding radius">
			<view>
				<view class="text-lg text-bold margin-bottom-xs">客户信息 </view>

				<view
					style="color: #999999;height: 100rpx; width: 600rpx;align-items: center;display: flex;border-top: #999999 solid 1rpx;">
					<image src="../../static/images/user.png" style="width: 48rpx;height:48rpx;margin-left:15rpx;">
					</image>
					<text style="margin-left: 10rpx;">{{orderList.phone}}</text>
				</view>
				<view
					style="color: #999999;height: 300rpx; width: 600rpx;display: flex;border-top: #999999 solid 1rpx;padding-top: 20rpx;">
					<view>
						<image src="../../static/images/address.png"
							style="width: 48rpx;height:48rpx;margin-left:15rpx;">
						</image>
					</view>
					<view style="margin-left: 10rpx;">
						<text>{{orderList.detailsAddress}}</text>
						<view>距您居住地址直线距离约为{{orderList.placeDistance}}</view>
						<view>距您当前直线距离约为{{orderList.distance}}</view>
						<view>距离信息仅供参考</view>
					</view>
					<view style="width: 280rpx;display: flex;">
						<image src="../../static/images/daohang.png"
							style="width: 48rpx;height:48rpx;margin-left:15rpx;">
						</image>
						<text style="color: #1afa29;" @click="bindGps()">查看导航</text>
					</view>
				</view>
				<view
					style="color: #999999;height: 100rpx; width: 600rpx;align-items: center;display: flex;border-top: #999999 solid 1rpx;">
					<image src="../../static/images/time.png" style="width: 48rpx;height:48rpx;margin-left:15rpx;">
					</image>
					<text style="margin-left: 10rpx;">{{orderList.deliveryTime}} 接单后尽快与业主联系 </text>
				</view>
			</view>
			<image src=".../../static/images/index/right.png" style="width: 15upx;height: 28upx;"></image>
		</view>

		<!-- <view class="flex align-center justify-between bg margin padding radius">
			<view>
				<view class="text-lg text-bold margin-bottom-xs">{{orderList.name}} {{orderList.phone}}</view>
				<view style="color: #999999;">{{orderList.province}} / {{orderList.city}} / {{orderList.district}} /
					{{orderList.detailsAddress}}
				</view>
			</view>
			<image src=".../../static/images/index/right.png" style="width: 15upx;height: 28upx;"></image>
		</view> -->

		<view class="bg padding radius margin" style="margin-bottom: 140rpx;">
			<view class="text-lg text-bold">
				订单信息
			</view>
			<view class="margin-right-xs">
				<view class="flex justify-between margin-top-lg" v-if="orderList.money">
					<view class="text-bold" style="width: 165rpx;" v-if="orderList.taskType==2">期望价格</view>
					<view class="text-bold" style="width: 165rpx;" v-else>价格</view>
					<view class="text-white" v-if="orderList.taskType!=2">¥{{orderList.rateMoney}}</view>
					<view class="text-white" v-else>¥{{orderList.money}}</view>
				</view>
				<view class="flex justify-between margin-top-lg">
					<view class="text-bold" style="width: 165rpx;">订单编号</view>
					<view @click="copyOrder(orderList.orderNo)" class="text-white">{{orderList.orderNo}}
						<u-icon name="order"></u-icon>
					</view>
				</view>
				<view class="flex justify-between margin-top-lg">
					<view class="text-bold" style="width: 165rpx;">下单时间</view>
					<view class="text-white">{{orderList.createTime}}</view>
				</view>

			</view>
		</view>

		<view class="btn" v-if="orderList.taskType==2" @click="openBaojia">立即报价</view>
		<view class="btn" v-else @click="jiedan(orderList)">立即接单</view>

		<!-- <view class="tabber">
			<view class="btn1">取消订单</view>
			<view class="btn2" @click="show = true">输入验证码</view>
		</view> -->
		<u-popup v-model="show" mode="center" border-radius="14" width="600rpx" :closeable="true">
			<view class="margin">
				<view class="text-center text-lg text-bold margin-top">报价</view>
				<view style="margin-top:40upx;">
					<input placeholder="请输入报价金额" name="input" v-model="offerMoney" type="text"
						style="background: #F2F2F2;height:100upx;padding:30upx"></input>
				</view>
				<view class="btn1" @click="offerBaojia">报价</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				helpOrderId: '',
				orderList: [],
				show: false,
				offerMoney: '',
				classify: 1,
				userId: '',
				arr: [],
				isSafetyMoney: 0,
				geRen: 0,
				onLine: 0,
				latitude: '',
				longitude: '',
			}

		},
		onLoad(option) {
			console.log(option)

			this.helpOrderId = option.id
			this.classify = uni.getStorageSync("classify")
			let that = this;
			if (this.helpOrderId) {
				// 获取用户当前位置 用于计算用户距离这个订单的距离
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						console.log("当前位置", res);
						that.latitude = res.latitude;
						that.longitude = res.longitude;
						that.getorderlist()
					}
				})
			}
			this.getRenZheng()
			// this.isSafetyMoney = uni.getStorageSync('isSafetyMoney') //是否缴纳保证金 0否 1是
			this.userId = uni.getStorageSync('userId')
			if (this.userId) {
				// this.$Request.getT('/app/common/type/235').then(res => { //新订单通知
				// 	if (res.code == 0) {
				// 		if (res.data && res.data.value) {
				// 			this.arr.push(res.data.value)
				// 		}
				// 	}
				// })
				// this.$Request.getT('/app/common/type/312').then(async res => { //师傅端订单状态通知
				// 	if (res.code == 0) {
				// 		if (res.data && res.data.value) {
				// 			await this.arr.push(res.data.value)
				// 		}
				// 	}
				// })
				this.$Request.getT('/app/common/type/261').then(res => { //商家私聊消息通知
					if (res.code == 0) {
						if (res.data && res.data.value) {
							this.arr.push(res.data.value)
						}
					}
				})

				this.$Request.getT('/app/common/type/313').then(res => { //订单状态通知 313
					if (res.code == 0) {
						if (res.data && res.data.value) {
							this.arr.push(res.data.value)
						}
					}
				})
			}
		},
		onShow() {


			this.getRenZheng()
			this.getUserInfo()
		},
		methods: {
			// 一键导航
			bindGps() {
				uni.openLocation({
					latitude: this.orderList.latitude - 0, //要去的纬度-地址       
					longitude: this.orderList.longitude - 0, //要去的经度-地址
					name: this.orderList.detailsAddress, //地址名称
					address: '', //详细地址名称
					success: function() {
						console.log('导航成功');
					},
					fail: function(error) {
						console.log(error)
					}
				});
			},
			copyOrder(order) {
				let that = this;
				uni.setClipboardData({
					data: order,
					success: function() {
						that.$queue.showToast('复制成功')
					}
				})
			},
			// 个人认证数据
			getRenZheng() {
				this.$Request.get("/app/userCertification/queryInsert").then(res => {
					if (res.code == 0 && res.data) {
						this.onLine = res.data.onLine
						if (res.data == null) {
							this.geRen = 0
							uni.setStorageSync("geRen", this.geRen)
						} else if (res.data.status == 0) {
							this.geRen = 1
							uni.setStorageSync("geRen", this.geRen)
						} else if (res.data.status == 1) {
							this.geRen = 2
							uni.setStorageSync("geRen", this.geRen)
						} else if (res.data.status == 2) {
							this.geRen = 3
							uni.setStorageSync("geRen", this.geRen)
						}
					}
				});
			},
			getUserInfo() {
				this.$Request.get("/app/user/selectUserById").then(res => {
					if (res.code == 0) {
						this.isSafetyMoney = res.data.isSafetyMoney
					}
				});
			},
			openBaojia() {
				this.$Request.get("/app/userCertification/queryInsert").then(res => {
					if (res.code == 0) {
						if (res.data == null) {
							uni.showModal({
								title: '提示',
								content: '您还未实名认证,请先认证',
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定');
										uni.navigateTo({
											url: '/my/renzheng/index'
										})
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							})
						} else if (res.data.status == 0) {
							uni.showModal({
								title: '提示',
								content: '实名认证审核中,请稍等',
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定');
										// uni.navigateTo({
										// 	url: '/my/renzheng/index'
										// })
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							})
						} else if (res.data.status == 1) {
							this.$Request.get("/app/common/type/616").then(res => {
								if (res.code == 0 && res.data && res.data.value != 0) {
									if (this.isSafetyMoney == 0) {
										uni.showModal({
											title: '提示',
											content: '您还未缴纳保证金，请先去缴纳保证金',
											success: function(re) {
												if (re.confirm) {
													uni.navigateTo({
														url: '/my/wallet/baozhengjin'
													})
												}
											}
										})
									} else {
										this.offerMoney = ''
										this.show = true
									}
								} else {
									this.offerMoney = ''
									this.show = true
								}
							});
						} else if (res.data.status == 2) {
							uni.showModal({
								title: '提示',
								content: '实名认证被拒绝,请先去认证',
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定');
										uni.navigateTo({
											url: '/my/renzheng/index'
										})
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							})
						}
					} else {
						this.checked = false
					}
				});

			},
			offerBaojia() {
				let that = this
				if (!that.offerMoney) {
					uni.showToast({
						title: '请输入报价金额',
						icon: 'none'
					})
					return
				}
				that.show = false
				let data = {
					id: that.orderList.id,
					offerMoney: that.offerMoney,
					userType: 3
				}
				that.$Request.post("/app/help/offerOrder", data).then(res => {
					if (res.code == 0) {
						that.offerMoney = ''
						uni.showToast({
							title: '报价成功'
						})
						//处理接单成功 返回首页数据刷新
						that.$queue.setData('jiedan', 'jiedan');
						setTimeout(function() {
							uni.redirectTo({
								url: '/my/takeOrder/baojia?index=0'
							})
						}, 1000)
					} else {
						that.offerMoney = ''
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			},
			//一键复制
			copyOrderuser(order) {
				let that = this;
				let data = '服务类型：' + order.orderTaking.gameId + '\n' + '服务地点：' + order.orderTaking.detailadd +
					'\n' +
					'服务用户：' + order.name + '\n' + '服务电话：' + order.phone + '\n' + '服务时间：' + order.startTime
				uni.setClipboardData({
					data: data,
					success: function() {
						that.$queue.showToast('复制成功')
					}
				})
			},
			// 查看图片
			saveImg(imgs, index) {
				if (this.LBSelect) {
					if (index == this.LBIndex - 1) {
						return;
					}
				}
				// console.log(imgs)
				let that = this;
				let imgArr = imgs
				// imgArr.push(imgs);
				// //预览图片
				uni.previewImage({
					urls: imgArr,
					current: imgArr[index]
				});
			},
			getorderlist() {
				this.$queue.showLoading("加载中...");
				let userId = this.$queue.getData('userId');
				let data = {
					helpOrderId: this.helpOrderId,
					userIds: userId,
					userId: uni.getStorageSync('userId'),
					latitude: this.latitude,
					longitude: this.longitude,
				}
				this.$Request.get("/app/help/selectHelpOrderDetails", data).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						this.orderList = res.data
						if (this.orderList.image) {
							this.orderList.image = this.orderList.image.split(',')
						}
						if (this.orderList.image) {
							this.orderList.serviceName = this.orderList.serviceName.split('/')
							this.orderList.serviceName = this.orderList.serviceName[0]
						}
						if (this.orderList.distance > 1000) {
							this.orderList.distance = Number((this.orderList.distance / 1000))
								.toFixed(2) + "km"
						} else {
							if (this.orderList.distance == '0') {
								this.orderList.distance = "0m";
							} else {
								this.orderList.distance = Number(this.orderList.distance).toFixed(
									1) + "m";
							}
						}

						if (this.orderList.placeDistance > 1000) {
							this.orderList.placeDistance = Number((this.orderList.placeDistance / 1000))
								.toFixed(2) + "km"
						} else {
							if (this.orderList.placeDistance == '0') {
								this.orderList.placeDistance = "0m";
							} else {
								this.orderList.placeDistance = Number(this.orderList.placeDistance).toFixed(
									1) + "m";
							}
						}
					}
				});
			},
			jiedan(order) {
				let that = this
				if (uni.getStorageSync('sendMsg')) {
					console.log('授权+1', JSON.stringify(that.arr))
					wx.requestSubscribeMessage({
						tmplIds: this.arr,
						success(re) {
							console.log(JSON.stringify(re), 111111111111)
							var datas = JSON.stringify(re);
							if (datas.indexOf("accept") != -1) {
								// console.log(re)
							}
						},
						fail: (res) => {
							console.log("订阅事变", res);
						}
					})
				}
				if (that.geRen == 2) {
					that.$Request.get("/app/common/type/616").then(res => {
						if (res.code == 0 && res.data && res.data.value != 0) {
							if (that.isSafetyMoney == 0) {
								uni.showModal({
									title: '提示',
									content: '您还未缴纳保证金，请先去缴纳保证金',
									success: function(re) {
										if (re.confirm) {
											uni.navigateTo({
												url: '/my/wallet/baozhengjin'
											})
										}
									}
								})

							} else {
								if (that.onLine == 1) {
									uni.showModal({
										title: '接单提示',
										content: '确认接单吗?',
										success: function(re) {
											if (re.confirm) {
												console.log('用户点击确定');
												let data = {
													orderId: order.id,
												}
												that.$Request.postJson(
														"/app/help/saveHelpTake", data)
													.then(res => {
														if (res.code == 0) {
															uni.showToast({
																title: '接单成功'
															})
															//处理接单成功 返回首页数据刷新
															that.$queue.setData(
																'jiedan',
																'jiedan');
															setTimeout(function() {
																uni.navigateTo({
																	url: '/my/takeOrder/index?index=0'
																})
															}, 1000)
														}
													});

											} else if (re.cancel) {
												console.log('用户点击取消');
											}
										}
									})
								} else {
									uni.showModal({
										title: '接单提示',
										content: '当前下线，是否上线接单?',
										success: function(re) {
											if (re.confirm) {
												console.log('用户点击确定');
												that.$Request.postJson(
													"/app/userCertification/updateCertification", {
														onLine: 1,
														userId: uni.getStorageSync(
															'userId')
													}).then(res => {
													if (res.code === 0) {
														uni.showToast({
															title: '上线成功',
															icon: 'none',
															duration: 1200
														})
														that.getRenZheng();
													} else {
														that.$queue.showToast(res
															.msg)
													}
												});
											} else if (re.cancel) {
												console.log('用户点击取消');
											}
										}
									})
								}
							}
						} else {
							uni.showModal({
								title: '接单提示',
								content: '确认接单吗?',
								success: function(re) {
									if (re.confirm) {
										console.log('用户点击确定');
										let data = {
											orderId: order.id,
											userType: 3
										}
										that.$Request.postJson("/app/help/saveHelpTake",
											data).then(
											res => {
												if (res.code == 0) {
													uni.showToast({
														title: '接单成功'
													})
													//处理接单成功 返回首页数据刷新
													that.$queue.setData('jiedan',
														'jiedan');
													setTimeout(function() {
														uni.navigateTo({
															url: '/my/takeOrder/index?index=0'
														})
													}, 1000)
												}
											});

									} else if (re.cancel) {
										console.log('用户点击取消');
									}
								}
							})
						}
					});

				} else if (that.geRen == 1) {
					uni.showModal({
						title: '提示',
						content: '实名认证审核中,请稍等',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定')
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				} else if (that.geRen == 3) {
					uni.showModal({
						title: '提示',
						content: '实名认证被拒绝,请修改后重新提交',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定')
								uni.navigateTo({
									url: '/my/renzheng/index?classify=' + that.classify
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '您还未实名认证,请先认证',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
								uni.navigateTo({
									url: '/my/renzheng/index?classify=' + that.classify
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				}
			},
			// 一键导航
			// bindGps(latitude, longitude, name) {
			// 	uni.openLocation({
			// 		latitude: latitude - 0, //要去的纬度-地址       
			// 		longitude: longitude - 0, //要去的经度-地址
			// 		name: name, //地址名称
			// 		address: name, //详细地址名称
			// 		success: function() {
			// 			console.log('导航成功');
			// 		},
			// 		fail: function(error) {
			// 			console.log(error)
			// 		}
			// 	});
			// },
			// 拨打电话
			bindphone(phone) {
				let that = this

				uni.showModal({
					title: '提示',
					content: '是否拨打电话',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定', that.phone);
							uni.makePhoneCall({
								phoneNumber: phone //仅为示例
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
		}
	}
</script>

<style>
	page {
		background: #F2F2F2;
	}

	.bg {
		background: #FFFFFF;
	}

	.box {
		flex: 1;
		text-align: center;
	}

	.active {
		background: #F2F5FF;
	}



	.btn {
		background-color: #557EFD;
		border-radius: 8upx;
		width: 90%;
		margin: 0 auto;
		color: #FFFFFF;
		text-align: center;
		padding: 25upx 0;
		position: fixed;
		bottom: 30upx;
		left: 0upx;
		right: 0upx;

	}

	.lianxi {
		background: #557EFD;
		color: #FFFFFF;
		display: inline-table;
		border-radius: 8upx;
		padding: 10upx 15upx;
	}

	.tabber {
		position: fixed;
		bottom: 0upx;
		left: 0upx;
		right: 0upx;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20upx 30upx;
	}

	.btn1 {
		margin-top: 60upx;
		height: 78upx;
		background: #557EFD;
		border-radius: 16upx;
		color: #FFFFFF;
		text-align: center;
		line-height: 78upx;
	}

	.btn2 {
		width: 330upx;
		height: 78upx;
		background: #557EFD;
		border-radius: 16upx;
		color: #FFFFFF;
		text-align: center;
		line-height: 78upx;
	}

	.btn3 {
		background: #557EFD;
		border-radius: 16upx;
		text-align: center;
		color: #FFFFFF;
		padding: 20rpx 10rpx;
		margin-top: 60rpx;
	}

	.wensheng {
		width: 230rpx;
	}
</style>